package com.sk.ccflower.server.lib.sockUtils.nio;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class NioServerSock extends BaseNioSock{

	private int bindPort;

	protected ServerSocketChannel ssc;

	protected Selector selector;

	public NioServerSock( int bindPort ) throws IOException{
		this.bindPort=bindPort;
		this.bind();

	}

	private void bind() throws IOException{
		super.selector=Selector.open();
		this.ssc=ServerSocketChannel.open();
		super.register( this.ssc , SelectionKey.OP_ACCEPT );
		ServerSocket ss=ssc.socket();
		InetSocketAddress address=new InetSocketAddress( this.bindPort );
		ss.bind( address );
		//this.log.debug("SERVER Ready...[" + this.bindPort + "]");
		System.out.println( "SERVER Ready...["+this.bindPort+"]" );
	}

	@SuppressWarnings("unused")
	private void accept( SelectionKey sKey ){

		try{
			SocketChannel sc=this.ssc.accept();

			super.register( sc , SelectionKey.OP_READ );
			System.out.println( "접속되었습니다..." );
		} catch( IOException ex ){
			//this.log.error(ex.toString() , ex);
			ex.printStackTrace();
		}

	}
}
